package com.hulk.dryad.admin.service.impl;

import com.fujieid.jap.sso.JapMfaService;
import com.hulk.dryad.persistence.entity.SysUserModel;
import com.hulk.dryad.admin.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *
 * @author kim
 * @date 2021/3/5
 */
@Service
@AllArgsConstructor
public class JapMfaServiceImpl implements JapMfaService {

	private final SysUserService sysUserService;

	/**
	 * 根据帐号查询 secretKey
	 *
	 * @param userName 申请 secretKey 的用户
	 * @return secretKey
	 */
	@Override
	public String getSecretKey(String userName) {
		SysUserModel sysUserModel = sysUserService.getUserByName(userName);
		return sysUserModel.getTotp();
	}

	/**
	 * 将 secretKey 关联 userName 后进行保存，可以存入数据库也可以存入其他缓存媒介中
	 *
	 * @param userName       用户名
	 * @param secretKey      申请到的 secretKey
	 * @param validationCode 当前计算出的 TOTP 验证码
	 * @param scratchCodes   scratch 码
	 */
	@Override
	public void saveUserCredentials(String userName, String secretKey, int validationCode, List<Integer> scratchCodes) {
		SysUserModel sysUserModel = sysUserService.getUserByName(userName);
		sysUserModel.setTotp(secretKey);
		sysUserService.updateById(sysUserModel);
	}

}
